#include "emb_task.h"

#define EMB_TASK_RUN(Id,Event) task_table[Id].Callback(Id,Event)

enum
{
    EMB_TASK_STATUS_STOP = 0,
    EMB_TASK_STATUS_RUNNING = 1,
};

static emb_task_id_t running_task_count = 0;
emb_task_ctrl_t task_table[EMB_CONF_TASK_NUM];

void emb_task_init()
{
    uint8_t id;

    for (id = 0; id < EMB_CONF_TASK_NUM; id++)
    {

    }
}

void emb_task_handler()
{
    emb_task_id_t id,idle_task_count;
    emb_event_iterator_t iterator;

    for (id = 0,idle_task_count=0; id < EMB_CONF_TASK_NUM;id++)
    {
        if(task_table[id].TaskStatus==EMB_TASK_STATUS_RUNNING) {

            if(_emb_event_iterator_init(id,&iterator)>0)
            {
                while(_emb_event_iterator_has_next(&iterator))
                {
                    task_table[id].Callback(id, _emb_event_ierator_next(iterator));
                }
            }
            else{
                idle_task_count++;
            }
        }
    }

    if (idle_task_count==running_task_count)
    {
        emb_idle_task();
    }
}

void emb_task_start(emb_task_id_t Id)
{
    EMB_PORT_ASSERT(Id < EMB_CONF_TASK_NUM);
    task_table[Id].TaskStatus = EMB_TASK_STATUS_RUNNING;
    running_task_count++;
}

void emb_task_stop(emb_task_id_t Id)
{
    EMB_PORT_ASSERT(Id < EMB_CONF_TASK_NUM);
    task_table[Id].TaskStatus = EMB_TASK_STATUS_STOP;
    running_task_count--;
}

void emb_idle_task()
{

}


